package ticketmachine.dataObjects;

import java.util.ResourceBundle;

import ticketmachine.gui.NewTicketView;
import ticketmachine.logic.TicketMachineFacade;

public class DAOFactory {
	
	private static DAOFactory factory;
	private ResourceBundle configuration;
	private IPromotionDAO promotionDAO;
	private IStationDAO stationDAO;
    private ITicketDescriptionDAO ticketDescriptionDAO;
	
	public DAOFactory() {
		try {
			configuration=ResourceBundle.getBundle("ticketmachine.configuration");
		} catch (Exception e) {
			System.out.println("Could not find Configuration File");
		}
	}

	public static DAOFactory getInstance(){
		if (factory==null){
			factory=new DAOFactory();
		}
		return factory;
	}

	public IPromotionDAO getPromotionDAO(){
		if (promotionDAO==null){
			String promotionDAOClass=configuration.getString("promotionDAOClass");
			try {
				promotionDAO=(IPromotionDAO) Class.forName(promotionDAOClass).newInstance();
			} catch (InstantiationException e) {
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			}
		}
		return promotionDAO;
	}
	
	public IStationDAO getStationDAO(){
		if (stationDAO==null){
			String stationDAOClass=configuration.getString("stationDAOClass");
			try {
				stationDAO=(IStationDAO)Class.forName(stationDAOClass).newInstance();
			} catch (InstantiationException e) {       
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			}
		}
		return stationDAO;		
	}
	
	public ITicketDescriptionDAO getTicketDescriptionDAO(){
		if (ticketDescriptionDAO==null){
			String ticketDescriptionDAOClass=configuration.getString("ticketDescriptionDAOClass");
			try {
				ticketDescriptionDAO=(ITicketDescriptionDAO) Class.forName(ticketDescriptionDAOClass).newInstance();
			} catch (InstantiationException e) {
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			}
		}
		return ticketDescriptionDAO;		
	}

}
